<?php

namespace api\modules\v1\controllers;
use api\modules\v1\controllers\UserController as U;
use Yii;
use yii\web\ServerErrorHttpException;
use yii\web\HttpException;
use yii\web\NotFoundHttpException;
class SimuserController extends U
{
    public $modelClass = 'api\modules\v1\models\Simuser';

    /*
     * 更换密码
     * @param $id
     * @return mixed
     */
    public function actionPwd(){
        $model = $this->findModel(Yii::$app->user->identity->id);
        $model->scenario='lr_pwd';
        $model->attributes = Yii::$app->request->post();
        $model->password_hash = Yii::$app->security->generatePasswordHash($model->password);//利用password_hash模式加密密码字符
        $model->access_token = $model->makeAccessToken($model->phone,$model->password);
        if ($model->save()) {
            return [
                "message"=>"密码更改成功,请重新登录!",
            ];
        }elseif (!$model->hasErrors()) {
            throw new ServerErrorHttpException('由于不明原因未能创建对象.');
        }else{
            throw new HttpException(401,array_values($model->getFirstErrors())[0],0);
        }
    }

}
